package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/employee")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable Long id){
        return employeeService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    @ApiOperation(value = "询所有对象")
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable Long id){
        employeeService.del(id);
        return new AjaxResult();
    }


    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        employeeService.patchDel(ids);
        return new AjaxResult();
    }

    /**
     * 接口：添加或修改
     * @param employee
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        if (employee.getId() == null) {//添加
            employeeService.add(employee);
        } else {
            employeeService.update(employee);
        }
        return new AjaxResult();
    }

    /**
     * 接口：分页+高级查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页+高级查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.list(shopId);
    }

}
